package com.sui.bigdata.rtcadmin.util;

import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author YongChen
 * @date 2019/11/6 14:41
 * @description
 * @email yong_chen@sui.com
 */
public class DateUtils {

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String formatDate(Date date, String formatStr) {
        if (date == null || StringUtils.isEmpty(formatStr)){
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat(formatStr);
        return dateFormat.format(date);
    }

    public static String getDifferentTime(Date beginTime, Date endTime) {
        if (beginTime == null || endTime == null){
            return null;
        }
        long diff = endTime.getTime() - beginTime.getTime();
        //计算天数
        long days = diff / (1000 * 60 * 60 * 24);
        //计算小时
        long hours = (diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        //计算分钟
        long minutes = (diff % (1000 * 60 * 60)) / (1000 * 60);
        //计算秒
        long seconds = (diff % (1000 * 60)) / 1000;

        return new StringBuffer().append(days).append("day;")
                                 .append(hours).append("h;")
                                 .append(minutes).append("m;")
                                 .append(seconds).append("s").toString();
    }
}
